package com.cloudfast.common.jdbc.reflect;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloudfast.common.date.DateUtils;
import com.cloudfast.dao.conf.EntityParseInfo;
import com.cloudfast.repository.annotation.DeleteField;
import com.cloudfast.repository.annotation.SelfGradeCode;
import com.cloudfast.repository.annotation.SelfInfo;
import com.cloudfast.repository.annotation.SubTable;

/**
 * @author liuyw
 * @描述:返回工具类
 * @date 2020年4月14日
 */
public class ReflectUtils<T> {
    private static final Logger logger = LoggerFactory.getLogger(Logger.class);

    private static final Pattern PATERRN_FORMAT = Pattern.compile("[A-Z]+");

    public ReflectUtils() {

    }

    /**
     * 判的是否为空
     *
     * @param obj
     * @return
     */
    private static boolean isNull(Object value) {
        if (value == null || value == "" || value == "null") {
            return true;
        }
        return false;
    }

    /**
     * 解析列名称-大写字段转_xxx
     *
     * @param fieldName
     * @return
     */
    public static String fieldNameToDataBaseName(String fieldName) {
        StringBuilder field = new StringBuilder();

        String[] fieldPattern = fieldName.split("");
        for (String s : fieldPattern) {
            Matcher m = PATERRN_FORMAT.matcher(s);
            if (m.matches() == true) {
                field.append("_").append(s.toLowerCase());
            } else {
                field.append(s);
            }
        }
        return field.toString();
    }

    /**
     * 是否是关联字段:是返回false: 否:返回true
     *
     * @param field
     * @return
     */
    private static boolean isNotLinkField(Field field) {
        Transient tst = field.getAnnotation(Transient.class);
        ManyToMany mtm = field.getAnnotation(ManyToMany.class);
        ManyToOne mto = field.getAnnotation(ManyToOne.class);
        OneToOne oto = field.getAnnotation(OneToOne.class);
        if (tst != null || mtm != null || mto != null || oto != null) {
            return false;
        }
        return true;
    }

    /**
     * 获取表名称
     *
     * @param clazz
     * @return
     */
    public static String getTableName(Class<?> clazz) {
        try {
            // 判断是否是(数据库)实体类
            Entity e = clazz.getAnnotation(Entity.class);
            // 获取表名
            Table table = clazz.getAnnotation(Table.class);
            // 是实体类
            if (e != null && table != null) {
                return table.name();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return null;
    }

    /**
     * 是否分表
     *
     * @param clazz
     * @return
     */
    public static boolean isSubTable(Class<?> clazz) {
        // 查询是否是分表
        SubTable subTable = clazz.getAnnotation(SubTable.class);
        if (subTable != null) {
            return true;
        }
        return false;
    }

    /**
     * 拼接分表
     *
     * @param entity
     * @return
     */
    public String findSubTableName(T entity) {
        String tableName = getTableName(entity.getClass());
        SubTable subTable = entity.getClass().getAnnotation(SubTable.class);
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMdd");
        String today = sdFormat.format(new Date());
        int length = 4;
        try {
            if (StringUtils.isNotEmpty(subTable.field())) {
                Object obj = getEntityFieldValue(entity, subTable.field());
                if (obj.getClass().getTypeName() == "java.util.Date") {
                    today = sdFormat.format(getDate(obj));
                } else if (obj.getClass().getTypeName() == "java.lang.String") {
                    today = obj.toString().replaceAll("-", "").replaceAll("-", "");
                }
            }
            switch (subTable.rule()) {
                case SubTable.MONTH:
                    length = 6;

                    break;
                case SubTable.YEAR:
                    length = 4;

                    break;
                case SubTable.DAY:
                    length = 8;

                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);

        }
        return tableName + "_" + today.substring(0, length);
    }

    /**
     * 获取主键-实体字段
     *
     * @param clazz
     * @return
     */
    public static String getPrimaryKeyField(Class<?> clazz) {
        String keyField = null;
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                Id id = field.getAnnotation(Id.class);
                if (id != null) {
                    keyField = field.getName();
                    break;
                }
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return keyField;
    }

    /**
     * 获取主键---数据库字段名称
     *
     * @param clazz
     * @return
     */
    public static String getPrimaryKeyColumn(Class<?> clazz) {
        String keyField = null;
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                Id id = field.getAnnotation(Id.class);
                if (id != null) {
                    keyField = fieldNameToDataBaseName(field.getName());
                    Column column = field.getAnnotation(Column.class);
                    if (column != null) {
                        keyField = column.name();
                    }
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return keyField;
    }

    /**
     * 获取主键的值
     *
     * @param entity
     * @return
     */
    public Object getPrimaryKeyValue(Object entity) {
        Object value = null;
        try {
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field field : fields) {
                Id id = field.getAnnotation(Id.class);
                if (id != null) {
                    field.setAccessible(true);
                    value = field.get(entity);
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    /**
     * 设置是否自定义自动主键生成
     *
     * @param entityClass
     * @return
     */
    public static boolean isGernerationId(Class<?> entityClass) {
        boolean isGernerationId = false;
        try {
            Field[] fields = entityClass.getDeclaredFields();
            for (Field field : fields) {
                Id id = field.getAnnotation(Id.class);
                if (id != null) {
                    GenericGenerator genericGenerator = field.getAnnotation(GenericGenerator.class);
                    if (genericGenerator != null) {
                        isGernerationId = true;
                    }
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return isGernerationId;
    }

    /**
     * 获取查询条件
     *
     * @param <T>
     * @param entity
     * @return
     */
    public static <T> Map<String, Object> selectWhereAndParams(T entity) {
        Map<String, Object> params = null;
        Map<String, Object> map = new HashMap<>();

        StringBuilder hql = new StringBuilder(" and ");
        try {
            int i = 0;
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field field : fields) {
                // 是否是关联字段:是返回false: 否:返回true
                if (isNotLinkField(field)) {
                    String _keyField = field.getName();
                    _keyField = fieldNameToDataBaseName(_keyField);
                    if (params == null) {
                        params = new LinkedHashMap<>();
                    }
                    field.setAccessible(true);
                    Object value = field.get(entity);
                    if (!isNull(value)) {
                        if (i > 0) {
                            hql.append(" and ");
                        }
                        hql.append(_keyField + "   = :").append(field.getName());
                        params.put(field.getName(), value);
                    }
                }
            }
            map.put("where", hql.toString());
            map.put("params", params);

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return map;
    }

    /**
     * 获取删除状态的字段--jpa 使用
     *
     * @param clazz
     * @return
     */
    public static Map<String, Object> getDeleteField(Class<?> clazz) {
        Map<String, Object> deleteField = null;
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                DeleteField _deleteField = field.getAnnotation(DeleteField.class);
                if (_deleteField != null) {
                    if (deleteField == null) {
                        deleteField = new LinkedHashMap<>();
                    }
                    deleteField.put(field.getName(), _deleteField.value());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return deleteField;
    }

    /**
     * 获取列名称
     *
     * @param clazz
     * @return
     */
    public static String getSelectColumns(Class<?> clazz) {
        StringBuilder keyField = new StringBuilder();
        int i = 0;
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                // 是否是关联字段:是返回false: 否:返回true

                if (isNotLinkField(field)) {
                    String _keyField = field.getName();
                    if (i > 0) {
                        keyField.append(",");
                    }
                    keyField.append(fieldNameToDataBaseName(_keyField)).append(" as ");
                    keyField.append("\"" + _keyField + "\"");
                    i++;
                }
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return keyField.toString();
    }

    /**
     * 获取除了 注键外的字段
     *
     * @param entity
     * @return
     */
    public Map<String, Object> getColumns(T entity) {
        Map<String, Object> params = null;
        try {
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field field : fields) {
                // 是否是关联字段:是返回false: 否:返回true
                if (isNotLinkField(field)) {
                    String _keyField = fieldNameToDataBaseName(field.getName());
                    field.setAccessible(true);
                    Object value = field.get(entity);
                    if (field.getType().getName().endsWith("java.util.Date") && value == null) {
                        value = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
                    }
                    if (params == null) {
                        params = new LinkedHashMap<>();
                    }
                    if (value != null && !"null".equals(value)) {
                        if (value instanceof java.util.Date) {
                            value = DateUtils.dateToString(getDate(value), "yyyy-MM-dd HH:mm:ss");
                        }
                        params.put(_keyField, value);
                    }

                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return params;
    }

    /**
     * 获取属性类型
     *
     * @param clazz
     * @return
     */
    public Map<String, String> getFieldType(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        Map<String, String> result = new HashMap<>();
        for (Field field : fields) {
            if (isNotLinkField(field)) {
                result.put(field.getName(), field.getType().getName());
            }
        }
        return result;
    }

    /**
     * 添加和修改的sql
     *
     * @param entity
     * @param tableName
     * @param primaryKey
     * @return
     */
    public Map<String, Object> repositoryHQL(T entity, String tableName, String primaryKey) {
        StringBuilder insert = new StringBuilder("insert into " + tableName + " (");
        StringBuilder insertValue = new StringBuilder();
        StringBuilder update = new StringBuilder("update " + tableName + " set ");
        StringBuilder where = new StringBuilder();
        Map<String, Object> params = new LinkedHashMap<>();

        Map<String, Object> SQLMAP = new LinkedHashMap<>();
        try {
            int i = 0;
            Field[] fields = entity.getClass().getDeclaredFields();
            // 获取主键值
            Object primaryValue = getPrimaryKeyValue(entity);

            for (Field field : fields) {
                // 是否是关联字段:是返回false: 否:返回true
                if (isNotLinkField(field)) {
                    String _keyField = fieldNameToDataBaseName(field.getName());
                    field.setAccessible(true);
                    Object value = field.get(entity);
                    if (field.getType().getName().endsWith("java.util.Date") && value == null) {
                        value = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
                    }
                    if ((value != null && !"null".equals(value)) && !(value.toString().isEmpty())) {
                        if (value instanceof java.util.Date) {
                            value = DateUtils.dateToString(getDate(value), "yyyy-MM-dd HH:mm:ss");
                        }
                        if (i > 0) {
                            insert.append(",");
                            insertValue.append(",");
                            update.append(",");
                            where.append(" and ");
                        }
                        insert.append(_keyField);
                        insertValue.append(" :" + field.getName());
                        update.append(_keyField).append(" = :" + field.getName());
                        where.append(_keyField).append(" = :" + field.getName());
                        params.put(field.getName(), value);
                        i++;
                    }
                }
            }
            insert.append(") values  (").append(insertValue).append(") ");
            update.append(" where  ").append(primaryKey).append("=").append(primaryValue);
            SQLMAP.put("update", update.toString());
            SQLMAP.put("insert", insert.toString());
            SQLMAP.put("where", where.toString());
            SQLMAP.put("params", params);

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return SQLMAP;
    }


    public Map<String, Object> repositorySQL(T entity, String tableName, String primaryKey) {
        StringBuilder insert = new StringBuilder("insert into " + tableName + " (");
        StringBuilder insertValue = new StringBuilder();
        StringBuilder update = new StringBuilder("update " + tableName + " set ");
        StringBuilder where = new StringBuilder();
        Map<String, Object> params = new LinkedHashMap<>();

        Map<String, Object> SQLMAP = new LinkedHashMap<>();
        try {
            int i = 0;
            Field[] fields = entity.getClass().getDeclaredFields();
            // 获取主键值
            Object primaryValue = getPrimaryKeyValue(entity);

            for (Field field : fields) {
                // 是否是关联字段:是返回false: 否:返回true
                if (isNotLinkField(field)) {
                    String _keyField = fieldNameToDataBaseName(field.getName());
                    field.setAccessible(true);
                    Object value = field.get(entity);
                    if (field.getType().getName().endsWith("java.util.Date") && value == null) {
                        value = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
                    }
                    if ((value != null && !"null".equals(value)) && !(value.toString().isEmpty())) {
                        if (value instanceof java.util.Date) {
                            value = DateUtils.dateToString(getDate(value), "yyyy-MM-dd HH:mm:ss");
                        }
                        if (i > 0) {
                            insert.append(",");
                            insertValue.append(",");
                            update.append(",");
                            where.append(" and ");
                        }
                        insert.append(_keyField);
                        insertValue.append(" ? ");
                        update.append(_keyField).append(" = ? ");
                        where.append(_keyField).append(" =  ? ");
                        params.put(field.getName(), value);
                        i++;
                    }
                }
            }
            insert.append(") values  (").append(insertValue).append(") ");
            update.append(" where  ").append(primaryKey).append("=").append(primaryValue);
            SQLMAP.put("update", update.toString());
            SQLMAP.put("insert", insert.toString());
            SQLMAP.put("where", where.toString());
            SQLMAP.put("params", params);

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return SQLMAP;
    }

    /**
     * object 转date
     *
     * @param o
     * @return
     */
    public Date getDate(Object o) {
        if (o == null || "".equals(o)) {
            return new Date();
        }
        if (o instanceof java.lang.String) {
            if (StringUtils.isNotEmpty(o.toString())) {
                return DateUtils.stringToDate(o.toString(), "yyyy-MM-dd HH:mm:ss");
            }
        }
        return (Date) o;
    }

    /**
     * 获取删除状态的字段
     *
     * @param clazz
     * @return
     */
    public static Map<String, Object> getDeleteColumn(Class<?> clazz) {
        Map<String, Object> deleteField = null;
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                DeleteField _deleteField = field.getAnnotation(DeleteField.class);
                if (_deleteField != null) {
                    if (deleteField == null) {
                        deleteField = new LinkedHashMap<>();
                    }
                    deleteField.put(field.getName(), _deleteField.value());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return deleteField;
    }

    /**
     * map 转bean
     *
     * @param map
     * @param clazz
     * @return
     */
    public T mapToBean(Map<String, Object> map, Class<T> clazz) {
        T entity = null;
        try {
            entity = clazz.newInstance();
            for (String key : map.keySet()) {
                Field f = clazz.getDeclaredField(key);
                f.setAccessible(true);
                Object value = map.get(key);
                if (value != null) {
                    if (f.getType().getName().endsWith("java.util.Date")) {
                        value = getDate(value);
                    }
                    f.set(entity, value);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return entity;
    }

    /**
     * 删除 多对多关联的信息
     *
     * @param clazz
     * @return
     */
    public static List<String> getDeleteJoinTable(Class<?> clazz) {
        List<String> list = new ArrayList<String>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            JoinTable joinTable = field.getAnnotation(JoinTable.class);
            if (joinTable != null) {
                StringBuilder hql = new StringBuilder("delete from ");
                hql.append(joinTable.name());
                JoinColumn joinColumn = joinTable.joinColumns()[0];
                hql.append(" where ").append(joinColumn.name());
                hql.append("=?");
                list.add(hql.toString());
            }
        }
        return list;
    }

    /**
     * 添加多对多的关系
     *
     * @param clazz
     * @return
     */
    public static String getInsertJoinTable(Class<?> clazz) {
        StringBuilder hql = new StringBuilder("");
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            JoinTable joinTable = field.getAnnotation(JoinTable.class);
            if (joinTable != null) {
                hql = new StringBuilder("insert into  ");
                hql.append(joinTable.name());
                JoinColumn joinColumn = joinTable.joinColumns()[0];
                JoinColumn inverseJoinColumns = joinTable.inverseJoinColumns()[0];
                hql.append("(").append(joinColumn.name()).append(",").append(inverseJoinColumns.name());
                hql.append(") values (?,?)");
            }
        }
        return hql.toString();
    }

    public List<Object> getJoinTable(T entity) {
        List<Object> list = new ArrayList<>();
        try {
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field field : fields) {
                JoinTable joinTable = field.getAnnotation(JoinTable.class);
                if (joinTable != null) {
                    field.setAccessible(true);
                    Object value = field.get(entity);
                    List<?> _list = (List<?>) value;
                    for (int i = 0; i < _list.size(); i++) {
                        Object obj = _list.get(i);
                        list.add(getPrimaryKeyValue(obj));
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return list;
    }

    /**
     * 获取主键返回的类型
     *
     * @param mEntityClass
     * @return
     */
    public Class<?> getPrimaryKeyType(Class<?> mEntityClass) {
        Class<?> keyField = null;
        try {
            Field[] fields = mEntityClass.getDeclaredFields();
            for (Field field : fields) {
                Id id = field.getAnnotation(Id.class);
                if (id != null) {
                    keyField = field.getType();
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return keyField;
    }

    /**
     * 获取自关联的值
     *
     * @param entity
     * @param mEntityClass
     * @return
     */
    public Object isSelfInfoKey(T entity, Class<?> mEntityClass) {
        Object obj = null;
        try {
            Field[] fields = mEntityClass.getDeclaredFields();
            for (Field field : fields) {
                SelfInfo mto = field.getAnnotation(SelfInfo.class);
                if (mto != null) {
                    obj = getColumnCompare(entity, mto.tableKey());
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return obj;
    }

    /**
     * 自连接关联
     *
     * @param entity
     * @param parent
     */
    public void fillSelfInfo(T entity, T parent) {
        try {
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field field : fields) {
                SelfInfo mto = field.getAnnotation(SelfInfo.class);
                if (mto != null) {
                    Field f = entity.getClass().getDeclaredField(mto.fileEntity());
                    f.setAccessible(true);
                    f.set(entity, parent);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    /**
     * 获取column 的值
     *
     * @param entity
     * @param column
     * @return
     */
    public Object getColumnCompare(T entity, String column) {
        Object value = null;
        try {
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field field : fields) {
                String _keyField = fieldNameToDataBaseName(field.getName());
                if (_keyField.equals(column)) {
                    field.setAccessible(true);
                    value = field.get(entity);
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    /**
     * 获取gradeCode 的父类id
     *
     * @param entity
     * @return
     */
    public Object getGradeParentId(T entity) {
        Object obj = null;
        try {
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field field : fields) {
                SelfGradeCode mto = field.getAnnotation(SelfGradeCode.class);
                if (mto != null) {
                    obj = getColumnCompare(entity, mto.parentKey());
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return obj;
    }

    /**
     * 获取
     *
     * @param parent
     * @return
     */
    public Object getParentGradeCode(T parent) {
        Object obj = null;
        try {
            Field[] fields = parent.getClass().getDeclaredFields();
            for (Field field : fields) {
                String _keyField = field.getName();
                SelfGradeCode mto = field.getAnnotation(SelfGradeCode.class);
                if (mto != null) {
                    if (_keyField.equals(mto.fileEntity())) {
                        field.setAccessible(true);
                        obj = field.get(parent);
                    }
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return obj;
    }

    /**
     * 给gradeCode 赋值
     *
     * @param entity
     * @param gradeCode
     */
    public Map<String, Object> fillGradeCode(T entity, String gradeCode) {
        Map<String, Object> uploadMap = new HashMap<>();
        uploadMap.put("update", false);
        try {
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field field : fields) {
                SelfGradeCode mto = field.getAnnotation(SelfGradeCode.class);
                if (mto != null) {
                    field.setAccessible(true);
                    Object entityGradeCode = field.get(entity);
                    if (entityGradeCode == null) {
                        if (StringUtils.isNotEmpty(gradeCode)) {
                            field.set(entity, gradeCode + getPrimaryKeyValue(entity) + "-");
                        } else {
                            field.set(entity, "-" + getPrimaryKeyValue(entity) + "-");
                        }
                        uploadMap.put("update", true);

                    } else if (!entityGradeCode.toString().equals(gradeCode + getPrimaryKeyValue(entity) + "-")) {
                        uploadMap.put("update", true);
                        field.set(entity, gradeCode + getPrimaryKeyValue(entity) + "-");
                    }

                    break;
                }
                uploadMap.put("data", entity);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return uploadMap;
    }

    /**
     * 获取实体类指定的field的值
     *
     * @param entity
     * @param field
     * @return
     */
    public Object getEntityFieldValue(T entity, String field) {
        Object value = null;
        try {
            Field[] fields = entity.getClass().getDeclaredFields();
            for (Field dfield : fields) {
                if (dfield.getName().equals(field)) {
                    dfield.setAccessible(true);
                    value = dfield.get(entity);
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return value;
    }

    public Map<String, Object> findEntityParams(T entity) {
        Map<String, Object> params = new LinkedHashMap<>();
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (isNotLinkField(field)) {
                field.setAccessible(true);
                Object value;
                try {
                    value = field.get(entity);
                    if ((value != null && !"null".equals(value)) && !(value.toString().isEmpty())) {
                        params.put(field.getName(), value);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return params;
    }

    /**
     * 解析entity 内容
     *
     * @param clazz
     * @return
     */
    public static EntityParseInfo parseEntityInfo(Class<?> clazz) {
        EntityParseInfo e = new EntityParseInfo();
        e.setPrimaryFieldKey(getPrimaryKeyField(clazz));
        e.setPrimaryColumnKey(getPrimaryKeyColumn(clazz));
        e.setTableName(getTableName(clazz));
        e.setSubTable(isSubTable(clazz));
        e.setSelectKey(getSelectColumns(clazz));
        e.setClazz(clazz);
        e.setDeleteKeys(getDeleteColumn(clazz));
        return e;
    }
}
